package com.css.zfzx.sjcj.modules.hddcGeochemicalAbnSegment.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.zfzx.sjcj.modules.hddcGeochemicalAbnSegment.repository.entity.HddcGeochemicalabnsegmentEntity;
import com.css.zfzx.sjcj.modules.hddcGeochemicalAbnSegment.service.HddcGeochemicalabnsegmentService;
import com.css.zfzx.sjcj.modules.hddcGeochemicalAbnSegment.viewobjects.HddcGeochemicalabnsegmentQueryParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhangcong
 * @date 2020-11-28
 */
@Slf4j
@RestController
@RequestMapping("/hddc/hddcGeochemicalabnsegments")
public class HddcGeochemicalabnsegmentController {
    @Autowired
    private HddcGeochemicalabnsegmentService hddcGeochemicalabnsegmentService;

    @GetMapping("/queryHddcGeochemicalabnsegments")
    public RestResponse queryHddcGeochemicalabnsegments(HttpServletRequest request, HddcGeochemicalabnsegmentQueryParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String sort= request.getParameter("sort");
            String order= request.getParameter("order");
            JSONObject jsonObject = hddcGeochemicalabnsegmentService.queryHddcGeochemicalabnsegments(queryParams,curPage,pageSize,sort,order);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getHddcGeochemicalabnsegment(@PathVariable String id) {
        RestResponse response = null;
        try{
            HddcGeochemicalabnsegmentEntity hddcGeochemicalabnsegment = hddcGeochemicalabnsegmentService.getHddcGeochemicalabnsegment(id);
            response = RestResponse.succeed(hddcGeochemicalabnsegment);
        }catch (Exception e){
            String errorMessage = "获取失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping
    public RestResponse saveHddcGeochemicalabnsegment(@RequestBody HddcGeochemicalabnsegmentEntity hddcGeochemicalabnsegment) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            hddcGeochemicalabnsegmentService.saveHddcGeochemicalabnsegment(hddcGeochemicalabnsegment);
            json.put("message", "新增成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }
    @PostMapping("/update")
    public RestResponse updateHddcGeochemicalabnsegment(@RequestBody HddcGeochemicalabnsegmentEntity hddcGeochemicalabnsegment)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            hddcGeochemicalabnsegmentService.updateHddcGeochemicalabnsegment(hddcGeochemicalabnsegment);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PostMapping("/delete")
    public RestResponse deleteHddcGeochemicalabnsegments(@RequestBody String ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            hddcGeochemicalabnsegmentService.deleteHddcGeochemicalabnsegments(Arrays.asList(ids.split(",")));
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(hddcGeochemicalabnsegmentService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }
    /***
     * 导出
     * @param response
     * @return
     */
    @GetMapping("/exportFile")
    public RestResponse exportFileYhDisasters(HttpServletResponse response,
                                              @RequestParam("projectName")String projectName, @RequestParam("targetfaultname") String targetfaultname,
                                              @RequestParam("province") String province, @RequestParam("city")String city, @RequestParam("area")String area,@RequestParam("qualityinspectionStatus")String qualityinspectionStatus) {
        RestResponse responseRest = null;
        JSONObject jsonObject = new JSONObject();
        try{
            HddcGeochemicalabnsegmentQueryParams queryParams=new HddcGeochemicalabnsegmentQueryParams();
            queryParams.setArea(area);
            queryParams.setCity(city);
            queryParams.setProvince(province);
            queryParams.setProjectName(projectName);
            queryParams.setTargetfaultname(targetfaultname);
            queryParams.setQualityinspectionStatus(qualityinspectionStatus);
            hddcGeochemicalabnsegmentService.exportFile(queryParams,response);
            jsonObject.put("message", "导出成功");
            responseRest = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            responseRest = RestResponse.fail(errorMessage);
        }
        return responseRest;
    }
    /**
     * 导入
     *
     * @param file
     * @param response
     * @return
     */
    @PostMapping("/importDisaster")
    public RestResponse export(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = hddcGeochemicalabnsegmentService.exportExcel( file, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}